package com.ethink.platform.manager;

import com.ethink.mdm.api.component.MdmComponent;
import com.ethink.order.api.info.*;
import com.ethink.warehouse.api.LogisticsTraceApplyOrderFacade;
import com.ethink.warehouse.api.enums.InventoryTypeEnum;
import com.ethink.warehouse.api.enums.LogisticsTraceDetailOrderTypeEnum;
import com.ethink.warehouse.api.enums.SubjectEnum;
import com.ethink.warehouse.api.request.LogisticsTraceApplyAddRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author default
 * @version 1.0.0
 * @ClassName TobSaleManager.java
 * @Description
 * @createTime 2022年11月17日 19:31:00
 */
@Component
public class SaleReturnManager {

    @Autowired
    private LogisticsTraceApplyOrderFacade logisticsTraceApplyOrderFacade;


    @Autowired
    private MdmComponent mdmComponent;

    /**
     * 预售退通过后创建待入申请
     *
     * @param tobSaleReturnOrderInfo
     * @param tobSaleReturnDetailOrderInfoList
     */
    public void t41(TobSaleReturnOrderInfo tobSaleReturnOrderInfo,
                    List<TobSaleReturnDetailOrderInfo> tobSaleReturnDetailOrderInfoList,
                    TobSaleOrderInfo tobSaleOrderInfo) {
        LogisticsTraceApplyAddRequest addRequest = new LogisticsTraceApplyAddRequest();
        addRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T1.getIntValue());
        addRequest.setSourceOrderNo(tobSaleReturnOrderInfo.getOrderNo());
        addRequest.setSourceOrderType(SubjectEnum.T41.getCode());
        addRequest.setSubject(SubjectEnum.T41.getCode());
        addRequest.setCompanyCode(null);
        addRequest.setCompanyName(tobSaleOrderInfo.getInSubWarehouseName());
        addRequest.setCreateUserCode(tobSaleReturnOrderInfo.getCreateUserCode());
        addRequest.setCreateUserName(tobSaleReturnOrderInfo.getCreateUserName());
        addRequest.setSaleReturnRkItems(tobSaleReturnDetailOrderInfoList.stream().map(detailInfo -> {
            LogisticsTraceApplyAddRequest.SaleReturnRkItem saleReturnRkItem = new LogisticsTraceApplyAddRequest.SaleReturnRkItem();
            saleReturnRkItem.setSourceDetailOrderNo(detailInfo.getDetailOrderNo());
            saleReturnRkItem.setMateSku(detailInfo.getMateSku());
            saleReturnRkItem.setApplyInQty(detailInfo.getReturnQty());
            if (StringUtils.isBlank(tobSaleOrderInfo.getSaleSubChannel())) {
                saleReturnRkItem.setSaleSubChannel("Z000");
            } else {
                saleReturnRkItem.setSaleSubChannel(tobSaleOrderInfo.getSaleSubChannel());
            }
            saleReturnRkItem.setSubWarehouseCode(tobSaleReturnOrderInfo.getSubWarehouseCode());
            saleReturnRkItem.setInventoryType(InventoryTypeEnum.ZP.getIntValue());
            return saleReturnRkItem;
        }).collect(Collectors.toList()));
        logisticsTraceApplyOrderFacade.addLogisticsTraceApplyOrder(addRequest);
    }

    /**
     * 销售退通过后创建待入申请
     */
    public void t71(SaleReturnOrderInfo saleReturnOrderInfo,
                    List<SaleReturnDetailOrderInfo> saleReturnDetailOrderInfoList,
                    SaleOrderInfo saleOrderInfo) {
        LogisticsTraceApplyAddRequest addRequest = new LogisticsTraceApplyAddRequest();
        addRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T1.getIntValue());
        addRequest.setSourceOrderNo(saleReturnOrderInfo.getOrderNo());
        addRequest.setSourceOrderType(SubjectEnum.T71.getCode());
        addRequest.setSubject(SubjectEnum.T71.getCode());
        addRequest.setCreateUserCode(saleReturnOrderInfo.getCreateUserCode());
        addRequest.setCreateUserName(saleReturnOrderInfo.getCreateUserName());
        addRequest.setCompanyCode(saleOrderInfo.getCustomerCode());
        addRequest.setCompanyName(saleOrderInfo.getCustomerName());
        addRequest.setSaleReturnRkItems(saleReturnDetailOrderInfoList.stream().map(detailInfo -> {
            LogisticsTraceApplyAddRequest.SaleReturnRkItem saleReturnRkItem = new LogisticsTraceApplyAddRequest.SaleReturnRkItem();
            saleReturnRkItem.setSourceDetailOrderNo(detailInfo.getDetailOrderNo());
            saleReturnRkItem.setMateSku(detailInfo.getMateSku());
            saleReturnRkItem.setApplyInQty(detailInfo.getReturnQty());
            if (StringUtils.isBlank(saleOrderInfo.getSaleSubChannel())) {
                saleReturnRkItem.setSaleSubChannel("Z000");
            } else {
                saleReturnRkItem.setSaleSubChannel(saleOrderInfo.getSaleSubChannel());
            }
            saleReturnRkItem.setSubWarehouseCode(saleReturnOrderInfo.getSubWarehouseCode());
            saleReturnRkItem.setInventoryType(InventoryTypeEnum.ZP.getIntValue());
            return saleReturnRkItem;
        }).collect(Collectors.toList()));
        logisticsTraceApplyOrderFacade.addLogisticsTraceApplyOrder(addRequest);
    }

}
